//
//  WHMsgCenterController.m
//  SquirrelSeller
//
//  Created by 邓伟浩 on 2019/1/7.
//  Copyright © 2019 邓伟浩. All rights reserved.
//

#import "WHMsgCenterController.h"
#import "WHMenuTipsCell.h"
#import "WHMsgHeaderView.h"
#import "WHMsgSendFailController.h"
#import "WHMsgChargeController.h"
#import "WHSendCountController.h"

@interface WHMsgCenterController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *dataTableView;

@property (nonatomic, strong) NSArray *tipsImageArray;
@property (nonatomic, strong) NSArray *tipsNameArray;
@property (nonatomic, strong) WHMsgHeaderView *tableHeaderView;

@end

@implementation WHMsgCenterController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self headerPage];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"短信中心";
    self.naviType = NaviTypeClear;
    [self initParams];
    [self setUpUI];
}

- (void)initParams {
    _tipsImageArray = @[@"msg_resend_icon", @"msg_charge_icon", @"msg_count_icon"];
    _tipsNameArray = @[@"发送失败", @"短信充值", @"发送统计"];
}

- (void)setUpUI{
    UITableView *dataTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    dataTableView.delegate = self;
    dataTableView.dataSource = self;
    [self.view addSubview:dataTableView];
    
    [dataTableView registerClass:[WHMenuTipsCell class] forCellReuseIdentifier:NSStringFromClass([WHMenuTipsCell class])];
    
    dataTableView.tableFooterView = [UIView new];
    dataTableView.tableHeaderView = [self tableHeaderView];
    dataTableView.backgroundColor = CViewBgMainColor;
    dataTableView.separatorColor = CLineColor;
    dataTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    dataTableView.bounces = NO;
    
    [dataTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.right.mas_equalTo(self.view);
    }];
    _dataTableView = dataTableView;
}

- (UIView *)tableHeaderView {
    WHMsgHeaderView *tableHeaderView = [[WHMsgHeaderView alloc] init];
    tableHeaderView.frame = CGRectMake(0, 0, KScreenWidth, kRealwidth(407));
    _tableHeaderView = tableHeaderView;
    
    return tableHeaderView;
}

#pragma mark - —————— UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _tipsNameArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WHMenuTipsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WHMenuTipsCell class]) forIndexPath:indexPath];
    
    NSString *tipName = _tipsNameArray[indexPath.row];
    cell.tipsImageView.image = IMAGE_NAMED(_tipsImageArray[indexPath.row]);
    cell.tipsLabel.text = tipName;
        
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *tipName = _tipsNameArray[indexPath.row];
    if ([tipName isEqualToString:@"发送失败"]) {
        WHMsgSendFailController *msgSendFail = [[WHMsgSendFailController alloc] init];
        [self.navigationController pushViewController:msgSendFail animated:YES];
    }
    if ([tipName isEqualToString:@"短信充值"]) {
        WHMsgChargeController *msgCharge = [[WHMsgChargeController alloc] init];
        [self.navigationController pushViewController:msgCharge animated:YES];
    }
    if ([tipName isEqualToString:@"发送统计"]) {
        WHSendCountController *sendCount = [[WHSendCountController alloc] init];
        [self.navigationController pushViewController:sendCount animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kRealwidth(85);
}

#pragma mark - —————— 接口 ——————
- (void)headerPage {
    NSString *postURL = NSStringFormat(@"%@%@", URL_main, URL_headPage);
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          @(userManager.detailUserInfo.userId), @"userId",
                          nil];
    
    [WHNetworkManager requestWithURL:postURL parameters:dict Success:^(id responseObject) {
        DLog(@"%@", responseObject);
        if (ValidDict(responseObject)) {
            NSNumber *code = responseObject[@"errcode"];
            NSString *errorMsg = responseObject[@"errmsg"];
            if (code.intValue == 0) {
                NSDictionary *retDict = responseObject[@"retobj"];
                
                WHMsgModel *msgModel = [WHMsgModel modelWithDictionary:retDict];
                _tableHeaderView.msgModel = msgModel;
                
            } else {
                [WHView showHudMsg:errorMsg];
            }
        }
    } failure:^(NSError *error) {
        
    }];
}

@end
